package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.vo.R;
import com.itheima.dto.page.MyPage;
import com.itheima.dto.advimg.YddAdvimgDto;
import com.itheima.entity.YddAdvimg;
import com.itheima.entity.YddAdvpos;
import com.itheima.service.YddAdvimgService;
import com.itheima.service.YddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告图管理
 *
 * @author zhangpei
 * @ClassName YddAdvimgController
 * @date 2022/5/27/19:15
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 广告图管理
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public R<MyPage<YddAdvimgDto>> list(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        //基础数据
        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> advimgWrapper = new LambdaQueryWrapper<>();
        advimgWrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc)
                .eq(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        yddAdvimgService.page(yddAdvimgPage, advimgWrapper);
        //设置MyPage的基本属性
        MyPage<YddAdvimgDto> myPage = new MyPage<>();
        //总条数
        myPage.setCount(yddAdvimgPage.getTotal());
        //每页条数
        myPage.setPageSize(yddAdvimgPage.getSize());
        //总页数
        myPage.setTotalPages((yddAdvimgPage.getTotal() / yddAdvimgPage.getSize()) + 1);
        //当前页
        myPage.setCurrentPage(yddAdvimgPage.getCurrent());
        List<YddAdvimg> advimgList = yddAdvimgPage.getRecords();
        List<YddAdvimgDto> advimgDtoList = new ArrayList<>();
        for (YddAdvimg yddAdvimg : advimgList) {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(yddAdvimg, yddAdvimgDto);
            YddAdvpos yddAdvpos = yddAdvposService.getById(yddAdvimg.getAdvimgpos());
            yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            advimgDtoList.add(yddAdvimgDto);
        }
        myPage.setData(advimgDtoList);
        return R.success(myPage);
    }

    /**
     * 添加广告图
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.save(yddAdvimg);
        return R.success(null);
    }

    /**
     * 删除广告图
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(Integer id) {
        yddAdvimgService.removeById(id);
        return R.success(null);
    }

    /**
     * 修改广告图 数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvimg> rowInfo(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return R.success(yddAdvimg);
    }

    /**
     * 广告图管理 修改广告图数据
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.updateById(yddAdvimg);
        return R.success(null);
    }

}
